import 'package:aitao_flutter/common_ui/age_widget.dart';
import 'package:aitao_flutter/common_ui/online_status.dart';
import 'package:aitao_flutter/common_ui/say_hello_btn.dart';
import 'package:aitao_flutter/common_ui/video_call_btn.dart';
import 'package:aitao_flutter/mine/controller/personal_controller.dart';
import 'package:aitao_flutter/res/r.dart';
import 'package:aitao_flutter/routes/routes.dart';
import 'package:aitao_flutter/style/box_style.dart';
import 'package:aitao_flutter/style/color_style.dart';
import 'package:aitao_flutter/style/edgeInsets_style.dart';
import 'package:aitao_flutter/style/radius_style.dart';
import 'package:aitao_flutter/style/text_style.dart';
import 'package:aitao_flutter/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper_null_safety_flutter3/flutter_swiper_null_safety_flutter3.dart';
import 'package:get/get.dart';
import '../../model/personal_model.dart';
import '../../utils/alert_utils.dart';

class PersonalPage extends StatelessWidget {
  PersonalPage({super.key});
  final PersonalController controller = Get.put(PersonalController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: ColorStyle.backGroundColor,
      body: GetBuilder<PersonalController>(
        builder: (controller) {
          return controller.personalModel == null
              ? const Center(
                  child: Text('加载中...'),
                )
              : Column(
                  children: [
                    Expanded(
                      child: CustomScrollView(
                        slivers: [
                          _headerWidget(),
                          _infoModuleWidget(),
                          _dynamicModuleWidget(),
                          _gravitationModuleWidget(),
                          _giftModuleWidget(),
                          _guardModuleWidget()
                        ],
                      ),
                    ),
                    _bottomWidget(context)
                  ],
                );
        },
        assignId: true,
      ),
    );
  }

  Widget _bottomWidget(BuildContext buildContext) {
    if (controller.userId == SpUtil.getUserData().userinfo?.userId) {
      debugPrint('查看自己主页');
      return Container();
    } else {
      debugPrint('查看别人主页');
      return Container(
        height: 80,
        color: Colors.white,
        // 男看女
        // 女看男
        child: controller.personalModel?.info?.gender == 1
            ? _manHomeBottom(buildContext)
            : _womenHomeBottom(buildContext),
      );
    }
  }

  /// 男看女
  Widget _manHomeBottom(BuildContext buildContext) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        GestureDetector(
          child: Image.asset(
            ImagePath.assetsShowChat,
            width: 44,
            height: 44,
          ),
          onTap: () {
            controller.chatClick(buildContext);
          },
        ),
        GestureDetector(
          child: Image.asset(
            ImagePath.assetsShowGift,
            width: 44,
            height: 44,
          ),
          onTap: () {
            AlertUtils().showGiftSheet(buildContext, () {
              Get.toNamed(Routes.rechargePage);
            }, (giftItem) {
              controller.sendGift(giftItem.id ?? 0);
            });
          },
        ),
        GestureDetector(
          child: Image.asset(
            ImagePath.assetsShowGuard,
            width: 44,
            height: 44,
          ),
          onTap: () {
            if (controller.personalModel != null &&
                controller.personalModel!.guard != null) {
              AlertUtils().showGuardDialog(
                  buildContext, controller.personalModel!.guard!,
                  (int guardNum) {
                debugPrint('守护数量:$guardNum');
                controller.sendGuard(guardNum);
              });
            }
          },
        ),
        VideoCallBtn(
          status: controller.personalModel?.userState == 3 ? true : false,
          videoCallBack: () {
            debugPrint('视频通话');
            controller.videoCheckCameraPermission();
          },
        )
      ],
    );
  }

  /// 女看男
  Widget _womenHomeBottom(BuildContext buildContext) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        SayHelloBtn(
          helloCallBack: () {
            debugPrint('打招呼');
            if (controller.personalModel != null &&
                controller.personalModel!.guard != null) {
              AlertUtils().showGreetSheet(buildContext, () {
                debugPrint('添加招呼');
                Get.toNamed(Routes.sayHelloPage);
              }, (greetText) {
                controller.greetTextClick(greetText);
              }, (greetImageId) {
                debugPrint('图片id:$greetImageId');
                controller.greetImageClick(buildContext, greetImageId);
              }, (greetVoiceId) {
                debugPrint('语音id:$greetVoiceId');
                controller.deleteGreetVoice(greetVoiceId);
              });
            }
          },
        ),
        VideoCallBtn(
          status: controller.personalModel?.userState == 3 ? true : false,
          videoCallBack: () {
            debugPrint('视频通话');
            controller.videoCheckCameraPermission();
          },
        )
      ],
    );
  }

  Widget _headerWidget() {
    return SliverToBoxAdapter(
      child: Container(
        // padding: EdgeInsetsStyle.edgeInsets(0, 0),
        height: 365.w,
        color: ColorStyle.orangeColor,
        child: Stack(
          children: [
            Positioned(
                top: 0,
                left: 0,
                right: 0,
                bottom: 0,
                child: Container(
                  color: ColorStyle.whiteColor,
                  child: Swiper(
                    itemBuilder: (BuildContext context, int index) {
                      return Image.network(
                        SpUtil.getImageUrl(
                            controller.personalModel!.album![index]),
                        fit: BoxFit.fill,
                      );
                    },
                    itemCount: controller.personalModel?.album?.length ?? 0,
                    pagination: const SwiperPagination(
                        builder: DotSwiperPaginationBuilder(
                            color: Color.fromRGBO(200, 200, 200, 0.5),
                            activeColor: ColorStyle.themeColor,
                            size: 8.0,
                            activeSize: 10.0)),
                  ),
                )),
            Positioned(
                top: 34,
                left: 16,
                child: GestureDetector(
                  child: const Icon(
                    Icons.arrow_back_ios_new_outlined,
                    color: ColorStyle.whiteColor,
                  ),
                  onTap: () {
                    Get.back();
                  },
                )),
            Positioned(
                top: 34,
                right: 16,
                child: GestureDetector(
                  child: const Icon(
                    Icons.more_horiz_sharp,
                    color: ColorStyle.whiteColor,
                  ),
                  onTap: () {
                    List<String> actions = [];
                    if (controller.isMinePage()) {
                      // 是自己主页
                      actions.add('编辑');
                    } else {
                      actions.add('举报');
                      actions.add('拉黑');
                    }
                    AlertUtils().showBottomListAlert(Get.context!, (e) {
                      controller.routerClick(e);
                    }, '', actions);
                  },
                )),
            // 魅力值
            Positioned(
                bottom: 10,
                right: 0,
                child: Container(
                  padding: EdgeInsetsStyle.edgeInsets(15, 7),
                  decoration: BoxDecoration(
                      borderRadius: RadiusStyle.setPortionRadius(
                          PortionRaduisType.raduisTopLeftAndBottomLeft, 21),
                      gradient: ColorStyle.charmValueColor),
                  child: Text(
                    '魅力值: ${controller.personalModel?.goodRate ?? ''}',
                    style: Styles.whiteText_15,
                  ),
                )),
            // 亲密等级
            controller.personalModel!.intimateLevel! > 0
                ? Positioned(
                    bottom: 10,
                    left: 18,
                    child: Row(
                      children: [
                        Image.asset(ImagePath.assetsIntimacyLogo),
                        Box.hBox6,
                        Text(
                          'LV.${controller.personalModel!.intimateLevel}${controller.personalModel!.intimateTitle ?? ''}',
                          style: Styles.whiteText_16,
                        )
                      ],
                    ))
                : Container()
          ],
        ),
      ),
    );
  }

  Widget _infoModuleWidget() {
    return SliverToBoxAdapter(
        child: Container(
      color: ColorStyle.whiteColor,
      padding: EdgeInsetsStyle.edgeInsets(16, 25),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                controller.personalModel?.info?.nickname ?? '',
                style: Styles.normalText_18,
              ),
              Box.hBox5,
              controller.personalModel?.info?.vip == 2
                  ? const SizedBox()
                  : Image.asset(
                      controller.setVipIcon(),
                      width: 30,
                      height: 30,
                    ),
              Box.hBox5,
              AgeWidget(
                  age: controller.personalModel?.info?.age ?? 18,
                  gender: controller.personalModel?.info?.gender ?? 1),
              Box.hBox5,
              OnlineStatus(status: controller.personalModel?.userState ?? 0),
              const Spacer(),
              controller.isMinePage()
                  ? Container()
                  : _focusBtnWidget(
                      controller.personalModel?.info?.isFollow ?? 0),
            ],
          ),
          Box.vBox3,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '${controller.personalModel?.info?.fansCount ?? 0}粉丝',
                style: Styles.greyText_12,
              ),
              controller.personalModel?.info?.gender == 2
                  ? Text(
                      'ID:${controller.personalModel?.info?.usercode}',
                      style: Styles.greyText_12,
                    )
                  : Container(),
            ],
          ),
          controller.personalModel?.info?.gender == 1
              ? _skillInfoWidget()
              : Container()
        ],
      ),
    ));
  }

  // 关注按钮 0:关注 1:取消关注
  Widget _focusBtnWidget(int focusStatus) {
    return GestureDetector(
      child: Container(
        width: 55.w,
        height: 25.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            borderRadius: RadiusStyle.radiusAll(10),
            color: ColorStyle.orangeColor),
        child: Text(
          focusStatus == 0 ? '+ 关注' : '已关注',
          style: Styles.whiteText_12,
        ),
      ),
      onTap: () {
        controller.focusClick();
      },
    );
  }

  // 视频收费设置展示
  Widget _skillInfoWidget() {
    if (controller.personalModel!.skill == null) {
      return Container();
    } else {
      PersonalSkill skill = controller.personalModel!.skill![0];
      return Container(
        // color: ColorStyle.orangeColor,
        margin: EdgeInsetsStyle.only(top: 5),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '视频单价:${skill.price}${skill.unit}/分钟',
              style: Styles.normalText_12,
            ),
            Box.vBox3,
            Text(
              '黄金会员:${skill.vipPrice}${skill.unit}/分钟',
              style: Styles.normalText_12,
            ),
            Box.vBox3,
            Text(
              '钻石会员:${skill.svipPrice}${skill.unit}/分钟',
              style: Styles.normalText_12,
            ),
            Box.vBox3,
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                controller.personalModel?.anchor?.orderSwitch == 2
                    ? Text(
                        '视频次数:${skill.serviceCount ?? 0}',
                        style: Styles.normalText_12,
                      )
                    : Container(),
                Text(
                  'ID:${controller.personalModel?.info?.usercode}',
                  style: Styles.greyText_12,
                ),
              ],
            )
          ],
        ),
      );
    }
  }

  // 个人动态
  Widget _dynamicModuleWidget() {
    return SliverToBoxAdapter(
      child: controller.dynamicsList.isEmpty
          ? Container()
          : Container(
              padding: EdgeInsetsStyle.edgeInsets(16, 25),
              margin: EdgeInsetsStyle.only(top: 14),
              color: ColorStyle.whiteColor,
              child: Column(
                children: [
                  _moduleTitleWidget('Ta的动态'),
                  Container(
                    padding: EdgeInsetsStyle.customMargin(16, 15, 16, 0),
                    child: Row(
                      children: controller.dynamicsList.map((e) {
                        return Container(
                            margin: EdgeInsetsStyle.only(right: 10),
                            child: Image.network(
                              SpUtil.getImageUrl(e),
                              width: 100.w,
                              height: 100.w,
                              fit: BoxFit.fitWidth,
                            ));
                      }).toList(),
                    ),
                  )
                ],
              ),
            ),
    );
  }

  // Ta的引力
  Widget _gravitationModuleWidget() {
    return SliverToBoxAdapter(
      child: controller.personalModel!.label!.isEmpty
          ? Container()
          : Container(
              padding: EdgeInsetsStyle.edgeInsets(16, 25),
              margin: EdgeInsetsStyle.only(top: 14),
              color: ColorStyle.whiteColor,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _moduleTitleWidget('Ta的引力', showArrow: false),
                  Container(
                    color: ColorStyle.whiteColor,
                    padding: EdgeInsetsStyle.customMargin(0, 15, 16, 0),
                    child: Wrap(
                        alignment: WrapAlignment.start,
                        spacing: 23,
                        runSpacing: 15,
                        children: controller.personalModel!.label!.map((e) {
                          return _gravitationItemWidget(
                              '${e.label}*${e.total}');
                        }).toList()),
                  )
                ],
              ),
            ),
    );
  }

  Widget _gravitationItemWidget(String title) {
    return Container(
      decoration: BoxDecoration(
          borderRadius: RadiusStyle.radiusAll(13.5),
          gradient: ColorStyle.gravitationItemColor),
      padding: EdgeInsetsStyle.edgeInsets(9.5, 4),
      child: Text(
        title,
        style: Styles.whiteText_13,
      ),
    );
  }

  // Ta获得的礼物
  Widget _giftModuleWidget() {
    return SliverToBoxAdapter(
        child: controller.giftList.isEmpty
            ? Container()
            : GestureDetector(
                onTap: () {
                  controller.routerClick('礼物列表');
                },
                child: Container(
                  padding: EdgeInsetsStyle.edgeInsets(16, 25),
                  margin: EdgeInsetsStyle.only(top: 14),
                  color: ColorStyle.whiteColor,
                  child: Column(
                    children: [
                      _moduleTitleWidget('Ta获得的礼物'),
                      Container(
                        padding: EdgeInsetsStyle.customMargin(0, 15, 16, 0),
                        child: Row(
                          children: controller.giftList.map((e) {
                            return Container(
                                margin: EdgeInsetsStyle.only(right: 10),
                                child: Image.network(
                                  SpUtil.getImageUrl(e),
                                  width: 60.w,
                                  height: 60.w,
                                  fit: BoxFit.fill,
                                ));
                          }).toList(),
                        ),
                      )
                    ],
                  ),
                ),
              ));
  }

  // Ta的守护
  Widget _guardModuleWidget() {
    return SliverToBoxAdapter(
      child: controller.guardList.isEmpty
          ? Container()
          : Container(
              padding: EdgeInsetsStyle.edgeInsets(16, 25),
              margin: EdgeInsetsStyle.only(top: 14),
              color: ColorStyle.whiteColor,
              child: Column(
                children: [
                  _moduleTitleWidget('Ta的守护'),
                  Container(
                    padding: EdgeInsetsStyle.customMargin(0, 15, 16, 0),
                    child: Row(
                      children: controller.guardList.map((e) {
                        return Container(
                            margin: EdgeInsetsStyle.only(right: 10),
                            child: Image.network(
                              SpUtil.getImageUrl(e),
                              width: 60.w,
                              height: 60.w,
                              fit: BoxFit.fill,
                            ));
                      }).toList(),
                      // children: [
                      //   Image.network(
                      //     'https://img0.baidu.com/it/u=2369424837,3677639351&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1698512400&t=54f98852f8cbc78e82397f9aa691cd74',
                      //     width: 60.w,
                      //     height: 60.w,
                      //     fit: BoxFit.fill,
                      //   ),
                      //   Box.hBox10,
                      //   Image.network(
                      //     'https://img0.baidu.com/it/u=2369424837,3677639351&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1698512400&t=54f98852f8cbc78e82397f9aa691cd74',
                      //     width: 60.w,
                      //     height: 60.w,
                      //     fit: BoxFit.fill,
                      //   )
                      // ],
                    ),
                  )
                ],
              ),
            ),
    );
  }

  Widget _moduleTitleWidget(String title, {bool showArrow = true}) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          title,
          style: Styles.normalText_16,
        ),
        showArrow
            ? const Icon(
                Icons.arrow_forward_ios_rounded,
                color: ColorStyle.aaaaaaColor,
              )
            : Container()
      ],
    );
  }
}
